define(['app','pagination','datetime','editor'], function( appCRM ) {
    'use strict';
     /*日程管理*/
    appCRM.controller('Event', function($scope, dataFactoryCRM, $q) {
      $scope.events;
      $scope.richtxtEdit = '';  //编辑日程，对应日程内容字段
      $scope.eventpage = {pageAll: 1,pageNow: 1, pageSize:12,k: 3};
      $scope.ownerpage = {pageAll: 1,pageNow: 1, pageSize:6,k: 3};
      $scope.ownerpage_all = {pageAll: 1,pageNow: 1, pageSize:6,k: 3};
      $scope.key;
      $scope.idss;

      $scope.chedownerid;
      $scope.chedowner;
      $scope.title_s;
      $scope.location_s;
      $scope.sectors;
      $scope.owners;
      $scope.owners_all;

      /*系统错误调用*/
      $scope.msgAttention = function() {
          $('.ui.systemerror').modal({
              closable: false,
              onDeny: function() {
                  window.alert('Wait not yet!');
                  return false;
              },
              onApprove: function() {
                  $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
              }
          }).modal('show');

          setTimeout(function() {
              var system = $('.ui.dimmer.modals.page.transition.visible.active');
              if (system) {
                  $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
              }
          }, 1000);
      };
        /*
        *按照搜索条件显示日程列表
        *$scope.key:日程查看方式切换：全部、我负责的、我下属负责的
        *$scope.idss:日程查看方式切换：今日、本周、本月
        */
      $scope.s_owner = "";
      $scope.s_title = "";
      $scope.EventListsShow = function(){
          $scope.$emit("BUSY");
          $scope.search_starttime = document.getElementById('starttime').value;
          var _url = 'event/index?p=' + $scope.eventpage.pageNow + '&psize=' + $scope.eventpage.pageSize;
          _url = _url + '&keys=' + $scope.key+'&id='+$scope.idss+ '&owner=' + $scope.s_owner + '&title='+$scope.s_title+ '&starttime=' + $scope.search_starttime;
          dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
              $scope.eventList_status = _d.rcode;
              $scope.eventpage.pageSize = _d.psize;
              $scope.eventpage.pageAll = _d.pall;
              $scope.eventpage.pageNow = _d.p;
              $scope.events = _d.data;
          });
          $scope.$emit("NOTBUSY");
      }
      /*
      *按照搜索条件显示日程列表
      */
      $scope.getEventList = function(key){
          if(key) $scope.key = key;
          $scope.EventListsShow();    
      }
      /*
      *显示今日、本周、本月日程
      */
      $scope.viewEventList = function(val){
          $scope.idss = val;
          $scope.getEventList($scope.key);
      }

      /*
      *获取当前用户所属部门及下属部门符合条件的负责人列表
      */
      $scope.nickname = "";
      $scope.owner_search_all = function(){
           var _url = 'Event/showUsers?p=' + $scope.ownerpage_all.pageNow + '&psize=' + $scope.ownerpage_all.pageSize;
             dataFactoryCRM.getDataByAPI( _url ).then(function(_d) {
                  if(_d.rcode=="1"){
                      $scope.owners_all = _d.data;
                      $scope.ownerpage_all.pageSize = _d.psize;
                      $scope.ownerpage_all.pageAll = _d.pall;
                      $scope.ownerpage_all.pageNow = _d.p;
                      $('#show_ownerss').hide();
                      $('#owner_list').hide();
                      $("#no_owner_lists").hide();
                      $("#all_owner_lists").show();
                      $('#show_ownerss_all').show();
                  }else{
                      $("#no_owner_lists").show();
                      $("#all_owner_lists").hide();
                      $('#show_ownerss').hide();
                      $('#owner_list').hide();
                      $('#show_ownerss_all').hide();
                  }
              });
          }

      /*
      *按照部门名、用户名搜索负责人
      *$scope.sectors:筛选日程负责人时，选择所属部门
      */
      $scope.nickname = "";
      $scope.owner_search = function(){
        $scope.owners = "";
        var _url = 'Event/showUsers?p=' + $scope.ownerpage.pageNow + '&psize=' + $scope.ownerpage.pageSize;
            _url += '&nickname='+ $scope.nickname + '&sectors=' + $scope.sectors;
         dataFactoryCRM.getDataByAPI( _url ).then(function(_d) {
              if(_d.rcode=="1"){
                  $scope.owners = _d.data;
                  $scope.ownerpage.pageSize = _d.psize;
                  $scope.ownerpage.pageAll = _d.pall;
                  $scope.ownerpage.pageNow = _d.p;
                  $('#show_ownerss').show();
                  $('#owner_list').show();
                  $("#no_owner_lists").hide();
                  $("#all_owner_lists").hide();
                  $('#show_ownerss_all').hide();
              }else{
                  $("#no_owner_lists").show();
                  $('#show_ownerss').hide();
                  $('#owner_list').hide();
                  $("#all_owner_lists").hide();
                  $('#show_ownerss_all').hide();
              }
          });
         $("#all_owner_lists").hide();
         $('#show_ownerss_all').hide();
      }

      /*
      *查看日程内容
      */
      $scope.showevents = function(){
          var _vevent = this.vevent;
          $scope.this_vevent =  this.vevent;
          $("#show_title").text(_vevent.title);
          $("#show_owner").text(_vevent.nickname);
          $("#show_location").text(_vevent.location);
          $("#show_starttime").text(_vevent.starttime);
          $("#show_endtime").text(_vevent.endtime);
          $scope.showevent_eventid = _vevent.event_id;
          $scope.showevent_ownerid = _vevent.owner_id;
          $("#viewEventContentss").text( _vevent.content);
          $('.ui.showevents.modal').modal('show');
      }

      /*
      *添加日程，选择负责人
      */
      $scope.showOwners = function(){
        var add_title = $scope.add_title;
        var add_location  = $scope.add_location;
        $scope.chedownerid = "";
        $scope.chedowner = "";
        $scope.owners = "";
        $scope.selOwnerBoxZero();
        $('#show_ownerss').hide();
        $('#owner_list').hide();
        $("#no_owner_lists").hide();
        $("#all_owner_lists").show();
        $("#show_ownerss_all").show();
            //选择日程负责人的单选按钮：取消选中状态
        $("input:radio[name='radio_owner']").attr("checked",false); 
        $("input:radio[name='radio_owner']").removeAttr('checked');
            //下拉按钮默认显示
        $('#seleOwner>.ui.selection.dropdown>.text').text("请选择日程负责人");
        $scope.sectors = "";
        $("#show_ownerss_all").show();
        $('.ui.selects').modal({
          onApprove: function() {
            $('#show_ownerss').show();
            var strdelid = $("input:radio[name='radio_owner']:checked").val();
              if(strdelid)  $scope.chedownerid = strdelid;
              else  return false;
              dataFactoryCRM.getDataByAPI('event/storOwner?strdelid='+strdelid ).then(function(d) {
                if(d.rcode){
                  $scope.chedowner = d.data['nickname'];
                }
                $('#frm-add input[name="owner"]').val($scope.chedowner);
                $('#frm-add input[name="owner_id"]').val($scope.chedownerid);
                $('.ui.add').modal({
                  onApprove:function(){
                    if(add_title && add_location){
                      $scope.add_title = add_title;
                      $scope.add_location = add_location;
                    }
                    if( $('#frm-add').form('is valid') ){
                        $scope.addEvent($('#frm-add').serialize() );
                    }else  return false;
                  }
                }).modal('show');
              })
          }
        }).modal('show');
          $scope.selOwnerBoxZero();//清空数据
          $scope.getEventList();//重新加载列表
          $scope.sectors = "";
      }

      /*
      *选择日程负责人时，获取当前用户可以分配日程的部门名称列表
      */
        $scope.sectors_w;
        $scope.busy = dataFactoryCRM.getDataByAPI('Event/getSector').then( function( data ) {
          $scope.busy = false;
              if( data ) {
                  $scope.sectors_w = data.psector;
              }
        });

      /*
      *日程管理搜索
      */
        $scope.search = function() {
            $scope.getEventList();
        }

      /*
      *添加日程界面
      */
      $scope.showAdd = function() {
        $scope.selOwnerBoxZero();
        $scope.addNewBoxZero();//清空数据
        $scope.chedownerid = "";
        $scope.chedowner = "";
          $('.ui.add').modal({
            onApprove: function() {
                if( $('#frm-add').form('is valid') ){
                    $scope.addEvent($('#frm-add').serialize() );
                }else  return false;
            }
          }).modal('show');
        }

      /*
      *添加日程
      */
      $scope.addEvent = function(_d) {
          $scope.$emit("BUSY");
          $scope.busy = dataFactoryCRM.postDataByAPI('event/add', _d).then(function(d) {
              $scope.eventRcode = d.rcode;
              $scope.eventctMsg = d.msg;
              if (d.rcode) {
                  $scope.EventListsShow();  //添加新日程后，按照搜索条件刷新
                  return true;
              } else {
                  $scope.msgAttention();
                  $scope.EventListsShow();  //添加新日程后，按照搜索条件刷新
                  return false;
              }
        });
      }
      /*
      *添加框清除默认值
      */
      $scope.addNewBoxZero=function(){
          $('#frm-add input').val("");
          $('#frm-add textarea').val("");
      }
      /*
      *选择负责人框清楚默认值
      */
      $scope.selOwnerBoxZero=function(){
        $scope.sectors = "";
        $scope.nickname = "";
      }
      $scope.chedownerid2;
      $scope.chedowner2;
      $scope.this_vevent;
      /*
      *编辑日程
      */
      $scope.edit_event = function(){
          $scope.$emit("BUSY");
          dataFactoryCRM.postDataByAPI('event/edit', $('#frm-edit').serialize() )
              .then(function(d) {
                $scope.eventRcode = d.rcode;
                $scope.eventctMsg = d.msg;
                  var _vevent = $scope.this_vevent;
                  if (d.rcode) {
                      if ($scope.events) {
                        var i = 0;
                        for (; i < $scope.events.length; i++) {
                            if ($scope.events[i].event_id == _vevent.event_id) {
                                $scope.events[i] = d.data;
                                break;
                            }
                        }
                       }
                      $scope.getEventList();//重新加载列表
                      return true;
                  } else {
                      $scope.msgAttention();
                      return false;
                  }
              });
          $scope.this_vevent = "";
          $scope.getEventList();//重新加载列表
      }
      /*
      *编辑日程，选择负责人
      *$scope.chedownerid2:编辑时，所选择日程负责人id
      */
      $scope.showOwners2 = function(){
          var _vevent = $scope.this_vevent;
          var title_s = $("#title").val();
          var location_s = $("#location").val();
          $scope.title_s = title_s;
          $scope.location_s = location_s;
          $scope.chedownerid2 = '';
          $scope.chedowner2 = '';
          $scope.owners = "";
          $scope.sectors = "";
          $scope.selOwnerBoxZero();
          $('#show_ownerss').hide();
          $('#owner_list').hide();
          $("#no_owner_lists").hide();
          $("#all_owner_lists").show();
          $('#show_ownerss_all').show();
              //选择日程负责人的单选按钮：取消选中状态
          $("input:radio[name='radio_owner']").attr("checked",false); 
          $("input:radio[name='radio_owner']").removeAttr('checked');
          $('.text').text("请选择日程负责人");
          $('#show_ownerss').hide();   //日程负责人筛选后的分页
          $('#no_owner_lists').hide();
          $('.ui.selects').modal({
              onApprove: function() {
                  $('#show_ownerss').show();
                  var strdelid2 = $("input:radio[name='radio_owner']:checked").map(function(index,elem) {
                      return $(elem).val();
                  }).get().join(',');
                  if(strdelid2)
                    $scope.chedownerid2 = strdelid2;
                  else
                    return false;
                  dataFactoryCRM.getDataByAPI('event/storOwner?strdelid='+strdelid2 ).then(function(d) {
                    if(d.rcode){
                      $scope.chedowner2 = d.data['nickname'];
                    }
                  $('#frm-edit input[name="owner_id"]').val($scope.chedownerid2);
                  $('#frm-edit input[name="owner"]').val($scope.chedowner2);
                  $('.ui.edit').modal({
                    onApprove:function(){
                      var _vevent = this.vevent;
                      $('#frm-edit input[name="owner_id"]').val($scope.chedownerid2);
                      $('#frm-edit input[name="owner"]').val($scope.chedowner2);
                      if( $('#frm-edit').form('is valid') ){
                          $scope.edit_event();  //编辑日程
                      }else
                         return false;
                      }
                  }).modal('show');
                })
            }
          }).modal('show');
            $scope.selOwnerBoxZero();//清空数据
            $scope.getEventList();//重新加载列表
        }

      /*
      *日程编辑
      */
      $scope.showEdit = function() {
          var _vevent = this.vevent;
          $scope.this_vevent =  this.vevent;
          $('#edit-sel').dropdown('set selected', _vevent.type);
          $('#frm-edit input[name="title"]').val(_vevent.title);
          $('#frm-edit input[name="owner"]').val(_vevent.nickname);
          $('#frm-edit input[name="location"]').val(_vevent.location);
          $('#frm-edit input[name="event_id"]').val(_vevent.event_id);
          $scope.richtxtEdit = _vevent.content ;
          $('#frm-edit input[name="starttime"]').val(_vevent.starttime);
          $('#frm-edit input[name="endtime"]').val(_vevent.endtime);
          $('#frm-edit input[name="owner_id"]').val(_vevent.owner_id);
          $('.ui.edit').modal({
              onApprove: function() {
                  $('#frm-edit input[name="content"]').val(  $scope.richtxtEdit );
                  if( $('#frm-edit').form('is valid') ){
                      $scope.edit_event();  //编辑日程
                  }else
                     return false;
              }
          }).modal('show');
      }
      /*
      *删除日程
      */
      $scope.delEvent = function() {
          var _vevent = this.vevent;
          $('#del-model .content p').html("确认删除日程 " + _vevent.title);
          $('.ui.small.del').modal({
              onDeny: function() {
                  $scope.$emit("BUSY");
                  dataFactoryCRM.getDataByAPI('event/remove?id=' + _vevent.event_id)
                      .then(function(d) {
                        $scope.eventRcode = d.rcode;
                        $scope.eventctMsg = d.msg;
                          if (d.rcode) {
                              if ($scope.events) {
                                  var i = 0;
                                  for (; i < $scope.events.length; i++) {
                                      if ($scope.events[i].event_id == _vevent.event_id) {
                                          $scope.events.splice(i, 1);
                                          break;
                                      }
                                  }
                              }
                              $scope.$emit("NOTBUSY");
                              return true;
                          } else {
                              $scope.$emit("NOTBUSY");
                              $scope.msgAttention();
                              return false;
                          }
                          
                      });
                  return true;
              }
          }).modal('show');
      }

      /*
      * 自动加载页面
      */
    $scope.adutoLoading = function(){
      $scope.getEventList(4);
      $scope.owner_search_all();
    };
    $scope.adutoLoading();

    });
});